package logic  
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import gc.events;
	import gc.states;
	import structures.Structure;
	import units.Unit;
	
	public class GameField extends Sprite 
	{
	
		private var mainList:Vector.<Structure>;
		private var checkList:Vector.<Structure>;
		private var stageRef:DisplayObjectContainer;
		private var statemachine:StateMachine;
		
		public function GameField(s:StateMachine) 
		{
			statemachine = s;
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		/*
		function collideVectorObject(v0:Vector.<Structure>, s:Structure):Function
		{
			var checkVectorObject:Function = new Function(item:Structure, index:int, vector:Vector.<Structure>):Boolean
			{
				return s.testCollide(item);
			}
			
			return checkVectorObject;
		}
		*/
		/*do this tomorrow
		function collideVectorVector(v0:Vector.<Structure>, v1:Vector.<Structure>):Function
		{
			var checkVectorVector = new Function(item:Structure, index:int, vector:Vector.<Structure>):Boolean
			{
				return v0.every(collideVectorObject(vector, item), 
			}
		}
		*/
		
		/*
		public function checkCollisionVectors(v0:Vector.<Structure>, v1:Vector.<Structure>):Structure
		{
			var flag:Boolean = false;
			var structA:Structure;
			var structB:Structure;
			
			//switch out for "every" mapping function
			
			for (var i:int = 0; i < v0.length; i++)
			{
				structA = v0[i];
				
				for (var j:int = 0; j < v1.length; j++)
				{
					structB = v1[j];
					
					if (structA.testCollide(structB))
					{
						return structA;
					}
				}
			}
			
			return null;
		}
		*/
		
		public function removeFromCheck(s:Structure):Structure
		{
			var struct:Structure = null;
			var index:int;
			try
			{
				index = checkList.indexOf(s);
				struct = checkList[index];
				checkList.splice(index, 1);
			}
			catch (err:Error)
			{
				trace(err);
			}
			
			return struct;
		}
		
		public function addCheck(s:Structure):void
		{
			checkList.push(s);
			stageRef.addChild(s);
		}
		
		public function addMain(s:Structure):void
		{
			mainList.push(s);
		}
		
		public function finalize(s:Structure):void
		{
			removeFromCheck(s);
			addMain(s);
		}
		
		/*
		public function push(u:Unit, add:Boolean):void
		{
		}
		*/
		
		private function nowbuilding_handler(e:CustomEvent):void
		{
			
			var struct:Structure;
			
			try 
			{
				struct = e.getArgs();
				addCheck(struct);
			}
			catch (err:Error)
			{
				trace(err);
			}
		}
		
		private function completebuild_handler(e:CustomEvent):void
		{
			try
			{
				finalize(e.getArgs());
				
			}
			catch (err:Error)
			{
				
			}
		}
		
		private function enterframe_handler(e:Event):void
		{
			
			
			for (var i:int = 0; i < checkList.length; i++)
			{
				//mainList.every(checkCollisions, checkList[i]);
			}
			
			/*
			//check collisions
			if (checkList.length > 0)
			{
				//assuming first one out is the current one building, will fix later
				var check:Structure = checkCollisionVectors(checkList, mainList);
				
				switch(statemachine.getState())
				{
					case states.NOW_BUILDING:
					trace(check)
				}
			}
			
			*/
		}
		
		
		private function init(e:Event):void
		{
			stageRef = parent;
			mainList = new Vector.<Structure>();
			checkList = new Vector.<Structure>();
			stageRef.addEventListener(events.NOW_BUILDING, nowbuilding_handler);
			stageRef.addEventListener(events.COMPLETE_BUILD, completebuild_handler);
			addEventListener(Event.ENTER_FRAME, enterframe_handler);
		}
		
	}

}